package edu.rmit.client;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.Calendar;
import java.util.List;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import edu.rmit.domain.Group;
import edu.rmit.domain.User;
import edu.rmit.util.CalendarUtil;

/**
 * This panel is used to display the review group request panel and the
 * corresponding control
 * 
 * @author Joey George
 * @version 1.0
 */
@SuppressWarnings("serial")
public class ReviewGroupRequestPanel extends JPanel {

	/**
	 * The window initialize height.
	 */
	private static int HEIGHT = 65;

	/**
	 * The window initialize width.
	 */
	private static int WIDTH = 500;

	/**
	 * The frame object to initialize the window.
	 */
	private JFrame frame;

	/**
	 * A list of panels which is combined by a group controls.
	 */
	private Vector<JPanel> groupPanels;

	/**
	 * A list of check box which combined by a group of check box.
	 */
	private Vector<JCheckBox> groupCheckBoxes;

	/**
	 * A list of label which combined by a group of group name.
	 */
	private Vector<JLabel> groupNames;

	/**
	 * A list of label which combined by a group of group descriptions.
	 */
	private Vector<JLabel> groupDescriptions;

	/**
	 * A list of label which combined by a group of proposal date.
	 */
	private Vector<JLabel> proposalDates;

	/**
	 * A list of label which combined by a group of creator.
	 */
	private Vector<JLabel> creators;

	/**
	 * The client instance which can get service provided by RMI service.
	 */
	private Client client;

	/**
	 * The button which clicked can make the administrator to grant the request.
	 */
	private JButton grantButton;

	/**
	 * The button which clicked can make the adminstrator to deny the request.
	 */
	private JButton denyButton;

	/**
	 * The cancel button which clicked can make the administrator to cancel the
	 * request.
	 */
	private JButton cancelButton;

	/**
	 * A list of groups which contains the groups need to be reviewed.
	 */
	private List<Group> newRequestedGroups = null;

	/**
	 * The parent frame instance.
	 */
	private JFrame parentFrame;

	/**
	 * The current user who want to perform this action.
	 */
	private User user;

	/**
	 * The constructor to initialize the review group panel.
	 * 
	 * @param parentFrame
	 *            The parent frame object to implement the parent frame refresh.
	 * @param user
	 *            The current user who want to implement this operation.
	 * @param client
	 *            The client instance which can get many service provided by RMI
	 *            service.
	 */
	public ReviewGroupRequestPanel(JFrame parentFrame, User user, Client client) {
		this.parentFrame = parentFrame;
		this.user = user;
		this.client = client;

		JPanel panelWhole = new JPanel();
		try {
			newRequestedGroups = client.getGroupService().getInterestGroups(
					false);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		int groupCount = newRequestedGroups.size();
		if (groupCount > 0) {
			panelWhole.setLayout(new GridLayout(groupCount + 1, 1));
			groupPanels = new Vector<JPanel>(groupCount);
			groupCheckBoxes = new Vector<JCheckBox>(groupCount);
			groupNames = new Vector<JLabel>(groupCount);
			groupDescriptions = new Vector<JLabel>(groupCount);
			proposalDates = new Vector<JLabel>(groupCount);
			creators = new Vector<JLabel>(groupCount);

			for (int i = 0; i < groupCount; i++) {
				groupCheckBoxes.add(new JCheckBox());
				groupNames.add(new JLabel(newRequestedGroups.get(i).getName()));
				groupDescriptions.add(new JLabel(newRequestedGroups.get(i)
						.getDescription()));
				Calendar createDate = newRequestedGroups.get(i).getCreateDate();
				proposalDates.add(new JLabel(CalendarUtil.valueof(createDate)));
				String creatorName = newRequestedGroups.get(i).getCreator();
				try {
					creators.add(new JLabel(client.getUserService().getUser(
							creatorName).getUserName()));
				} catch (RemoteException e) {
					e.printStackTrace();
				}
				groupPanels.add(new JPanel());
				groupPanels.get(i).setLayout(new GridLayout(1, 4));
				groupPanels.get(i).add(groupCheckBoxes.get(i));
				groupPanels.get(i).add(groupNames.get(i));
				groupPanels.get(i).add(groupDescriptions.get(i));
				groupPanels.get(i).add(proposalDates.get(i));
				groupPanels.get(i).add(creators.get(i));
				panelWhole.add(groupPanels.get(i));
			}
			grantButton = new JButton("Grant");
			denyButton = new JButton("Deny");
			cancelButton = new JButton("Cancel");

			grantButton.addActionListener(new ListenerGrantGroupRequest());
			denyButton.addActionListener(new ListenerDenyGroupRequest());

			JPanel operationPanel = new JPanel();
			operationPanel.setLayout(new GridLayout(1, 5));
			operationPanel.add(new JLabel(""));
			operationPanel.add(grantButton);
			operationPanel.add(denyButton);
			operationPanel.add(cancelButton);
			operationPanel.add(new JLabel(""));
			panelWhole.add(operationPanel);
			panelWhole.setBorder(BorderFactory
					.createTitledBorder("All new requested groups"));
		}
		frame = new JFrame("User Register Window");
		frame.setContentPane(panelWhole);
		initFrame(groupCount);
	}

	/**
	 * This method is to implement initialize the frame instance.
	 * 
	 * @param groupCount
	 *            The group size of the group need to be reviewed.
	 */
	private void initFrame(int groupCount) {
		frame.setResizable(true);
		frame.setVisible(true);
		frame.setSize(WIDTH, HEIGHT * groupCount + 30);

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = frame.getSize();
		if (frameSize.height > screenSize.height) {
			frameSize.height = screenSize.height;
		}
		if (frameSize.width > screenSize.width) {
			frameSize.width = screenSize.width;
		}
		frame.setLocation((screenSize.width - frameSize.width) / 2,
				(screenSize.height - frameSize.height) / 2);
	}

	/**
	 * 
	 * This class is to implement the grant group request.
	 * 
	 * @author Joey George
	 * @version 1.0
	 */
	class ListenerGrantGroupRequest implements ActionListener {
		/**
		 * This method is to implement the grant group request logic.
		 */
		@Override
		public void actionPerformed(ActionEvent e) {
			for (int index = 0; index < groupCheckBoxes.size(); index++) {
				if (groupCheckBoxes.get(index).isSelected()) {
					Group group = newRequestedGroups.get(index);
					try {
						client.getGroupService().changeGroupStatus(
								group.getId(), true);
						newRequestedGroups.remove(index);
						groupPanels.remove(index);
						groupCheckBoxes.remove(index);
						index--;
					} catch (RemoteException e1) {
						e1.printStackTrace();
					}
				}
			}
			frame.dispose();
			parentFrame.dispose();
			new GroupListPanel(client, user);
		}
	}

	/**
	 * This class is to implement the logic when user clicked the deny group
	 * operation.
	 * 
	 * @author Joey George
	 * @version 1.0
	 * 
	 */
	class ListenerDenyGroupRequest implements ActionListener {
		/**
		 * The logic which when the deny group button is been clicked.
		 */
		@Override
		public void actionPerformed(ActionEvent e) {
			for (int index = 0; index < groupCheckBoxes.size(); index++) {
				if (groupCheckBoxes.get(index).isSelected()) {
					Group group = newRequestedGroups.get(index);
					try {
						client.getGroupService().removeGroup(group.getId());
						newRequestedGroups.remove(index);
						groupPanels.remove(index);
						groupCheckBoxes.remove(index);
						index--;
					} catch (RemoteException e1) {
						e1.printStackTrace();
					}
				}
			}
			frame.dispose();
			parentFrame.dispose();
			new GroupListPanel(client, user);
		}

	}
}
